package com.xiaotu.controller;

import com.xiaotu.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Method;
import java.util.List;

@Controller
public class DeptConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    //Ribbon 我们这里的地址应该是一个变量,通过服务名来访问

    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDE-DEPT";
    //private static final String REST_URL_PREFIX = "http://localhost:8001";

    @RequestMapping(path = "/dept/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Dept getDeptById(@PathVariable("id") Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/"+id,Dept.class);
    }
    @RequestMapping(path = "/dept",method = RequestMethod.POST)
    @ResponseBody
    public Boolean addDept(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept", dept, Boolean.class);
    }
    @RequestMapping(path = "/dept",method = RequestMethod.GET)
    @ResponseBody
    public List<Dept> deptList(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept", List.class);
    }
}
